package com.faithbj.service.statistics;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.faithbj.common.model.CheckTotal;
import com.faithbj.common.utils.DateUtils;
import com.faithbj.process.State;
import com.faithbj.process.stats.JbpmStatsService;
import com.faithbj.process.stats.PauseReason;
import com.faithbj.process.stats.TerminateReason;
import com.faithbj.service.CheckTotalService;
import com.faithbj.service.RegionService;
import com.faithbj.support.fp.Tuple3;
import com.faithbj.support.fp.Tuple4;
import com.faithbj.support.fp.Tuple6;

/**
 * 测试用
 * @author rym
 *
 */
@Service("testJbpmStatsService")
public class TestJbpmStatsService implements JbpmStatsService{

	@Resource
	RegionService regionService;
	@Resource
	CheckTotalService checkTotalService;
	 /**
     * 1.请求参数：申请ID列表；响应参数：每个申请的当前状态、首核责任人、受理日期、街镇审核日期、导入内网日期、状态的起始日期.
     * @param applyIds 申请ID列表
     * @return Map(申请ID, 元组). 元组定义: (申请状态, 首核责任人名字, 受理日期, 街镇审核日期, 导入内网日期, 状态的起始日期)
     */
	@Override
	public Map<String, Tuple6<State, String, Date, Date, Date, Date>> getApplyDetailsByApplyIds(
			List<String> applyIds) {
		// TODO Auto-generated method stub
		String name = "浩";
		Map<String, Tuple6<State, String, Date, Date, Date, Date>> resultMap = new HashMap<String, Tuple6<State, String, Date, Date, Date, Date>>();
		for(String applyId : applyIds){
			Tuple6 tuple6 = new Tuple6(State.INTERRUPTED, name, new Date(), new Date(), new Date(), new Date());
			resultMap.put(applyId, tuple6);
		}
		return resultMap;
	}

	/**
     * 2.请求参数：regionId区域ID、操作时间区间；响应参数：本区域内各个状态下的数目.
     * @param regionId 区域ID. 有可能是市级的，也有可能是区县的，也有可能是街镇的
     * @param from 操作时间区间下限
     * @param to 操作时间区间上限
     * @return Map(区域ID, Map(状态, 数目))
     */
	@Override
	public Map<String, Map<State, Long>> countGroupByApplyStateInRegion(
			String regionId, Date from, Date to) {
		// TODO Auto-generated method stub
		Map<State, Long> stateMap = new HashMap<State, Long>();
		stateMap.put(State.INTERRUPTED, new Long(1));
		stateMap.put(State.DIFF_TABLE_GENERATED, new Long(2));
		stateMap.put(State.CHECK_TABLE_GENERATED, new Long(3));
		stateMap.put(State.TERMINATED, new Long(4));
		stateMap.put(State.COMPARISON_DONE, new Long(5));
		stateMap.put(State.FIRST_CHECK, new Long(6));
		stateMap.put(State.SECOND_CHECK, new Long(7));
		stateMap.put(State.REPORT, new Long(8));
		stateMap.put(State.COMPLETION, new Long(9));
		stateMap.put(State.VERIFICATION, new Long(10));
		List<String> streetRegionIdList = regionService.getStreetRegionIdById(regionId);
		Map<String, Map<State, Long>> resultMap = new HashMap<String, Map<State, Long>>();
		for(String streetRegionId : streetRegionIdList) {
			resultMap.put(streetRegionId, stateMap);
		}
		return resultMap;
	}

	 /**
     * 3.请求参数：regionId区域Id（有可能是区县的也有可能是街镇的，如果是区县的，返回里要有街镇ID）、
     * 操作时间区间；响应参数：出具报告总数；只超收入报告数据；只超财产报告数；收入和财产均超报告数.
     * @param regionId 区域ID
     * @param from 操作时间区间下限
     * @param to 操作时间区间上限
     * @return Map(区域ID, 元组). 元组定义: (出具报告总数, 只超收入报告数据, 只超财产报告数, 收入和财产均超报告数)
     */
	@Override
	public Map<String, Tuple4<Long, Long, Long, Long>> countReportsGroupByStreetInRegion(
			String regionId, Date from, Date to) {
		// TODO Auto-generated method stub
		List<String> streetRegionIdList = regionService.getStreetRegionIdById(regionId);
		Map<String, Tuple4<Long, Long, Long, Long>> resultMap = new HashMap<String, Tuple4<Long, Long, Long, Long>>();
		Tuple4 tuple4 = new Tuple4(new Long("10"), new Long("10"), new Long("10"), new Long("10"));
		for(String streetRegionId : streetRegionIdList) {
			resultMap.put(streetRegionId, tuple4);
		}
		return resultMap;
	}

	 /**
     * 4.请求参数：中止核对操作日期区间、regionId区域ID；响应参数：中止原因列表（包括中止原因及中止家庭户数）.
     * @param regionId 区域ID
     * @param from 中止核对操作日期区间下限
     * @param to 中止核对操作日期区间上限
     * @return Map(中止原因, 对应的家庭户数)
     */
	@Override
	public Map<PauseReason, Long> getPauseReasonDetailsInRegion(
			String regionId, Date from, Date to) {
		// TODO Auto-generated method stub
		PauseReason reason1 = new PauseReason();
		reason1.setId(new Long("1"));
		reason1.setName("原因1");
		PauseReason reason2 = new PauseReason();
		reason2.setId(new Long("2"));
		reason2.setName("原因2");
		PauseReason reason3 = new PauseReason();
		reason3.setId(new Long("3"));
		reason3.setName("原因3");
		Map<PauseReason, Long> resultMap = new HashMap<PauseReason, Long>();
		resultMap.put(reason1, new Long("10"));
		resultMap.put(reason2, new Long(20));
		resultMap.put(reason3, new Long("30"));
		return resultMap;
	}

	@Override
	public Map<TerminateReason, Long> getTerminateReasonDetailsInRegion(
			String regionId, Date from, Date to) {
		// TODO Auto-generated method stub
		TerminateReason reason1 = new TerminateReason();
		reason1.setId(new Long("1"));
		reason1.setName("原因1");
		TerminateReason reason2 = new TerminateReason();
		reason2.setId(new Long("2"));
		reason2.setName("原因2");
		TerminateReason reason3 = new TerminateReason();
		reason3.setId(new Long("3"));
		reason3.setName("原因3");
		Map<TerminateReason, Long> resultMap = new HashMap<TerminateReason, Long>();
		resultMap.put(reason1, new Long("10"));
		resultMap.put(reason2, new Long(20));
		resultMap.put(reason3, new Long("30"));
		return resultMap;
	}

	/**
     * 6.请求参数：操作截止时间区间；返回操作截止时间在请求时间区间内的所有操作人在
     * （准入标准审核、比对审核、抽查）三个状态结点的状态开始时间、状态截止时间、实际操作时间），
     * 如果实际还未操作，则返回当前时间或者返回为空。注：只返回状态截止时间早于实际操作时间，即已经超时的数据.
     * @param from 操作截止时间区间下限
     * @param to 操作截止时间区间上限
     * @return Map(操作人名字, Map(状态, 元组列表)). 状态为标准审核、比对审核、抽查三个状态. 元组定义: (开始时间, 截止时间, 操作时间)
     */
	@Override
	public Map<String, Map<State, List<Tuple3<Date, Date, Date>>>> getTimingDetailsInVerifyPhaseDeadlineDuring(
			Date from, Date to) {
		// TODO Auto-generated method stub
		Date start = DateUtils.getStringToDate("2012-05-20 12:12:12","yyyy-MM-dd HH:mm:ss");
		Date end = DateUtils.getStringToDate("2012-06-20 12:12:12","yyyy-MM-dd HH:mm:ss");
		Date operate = DateUtils.getStringToDate("2012-06-26 12:12:12","yyyy-MM-dd HH:mm:ss");
		List<Tuple3<Date, Date, Date>> tuple3List = new ArrayList<Tuple3<Date, Date, Date>>();
		for(int i = 0; i < 10; i++) {
			Tuple3 tuple3 = new Tuple3(start, end, operate);
			tuple3List.add(tuple3);
		}
		Map<State, List<Tuple3<Date, Date, Date>>> stateMap = new HashMap<State, List<Tuple3<Date, Date, Date>>>();
		stateMap.put(State.COMPARISON_DONE, tuple3List);
		stateMap.put(State.FIRST_CHECK, tuple3List);
		stateMap.put(State.SECOND_CHECK, tuple3List);
		Map<String, Map<State, List<Tuple3<Date, Date, Date>>>> resultMap = new HashMap<String, Map<State, List<Tuple3<Date, Date, Date>>>>();
		resultMap.put("张三", stateMap);
		resultMap.put("李四", stateMap);
		resultMap.put("王五", stateMap);
		return resultMap;
	}

	/**
     * 7.请求参数：操作截止时间区间，regionId区域ID；返回操作截止时间在请求时间区间内的每个regionId在
     * （信息录入、信息核实）两个状态结点的状态开始时间、状态截止时间、实际操作时间），如果实际还未操作，
     * 则返回当前时间或者返回为空.注：只返回状态截止时间早于实际操作时间，即已经超时的数据.
     * @param regionId 区域ID
     * @param from 操作截止时间区间下限
     * @param to 操作截止时间区间上限
     * @return Map(区域ID, Map(状态, 元组列表)). 状态为信息录入、信息核实两个状态. 元组定义: (开始时间, 截止时间, 操作时间)
     */
	@Override
	public Map<String, Map<State, List<Tuple3<Date, Date, Date>>>> getTimingDetailsInInputPhaseDeadlineDuring(
			String regionId, Date from, Date to) {
		// TODO Auto-generated method stub
		Date start = DateUtils.getStringToDate("2012-05-20 12:12:12","yyyy-MM-dd HH:mm:ss");
		Date end = DateUtils.getStringToDate("2012-06-20 12:12:12","yyyy-MM-dd HH:mm:ss");
		Date operate = DateUtils.getStringToDate("2012-06-26 12:12:12","yyyy-MM-dd HH:mm:ss");
		List<Tuple3<Date, Date, Date>> tuple3List = new ArrayList<Tuple3<Date, Date, Date>>();
		for(int i = 0; i < 10; i++) {
			Tuple3 tuple3 = new Tuple3(start, end, operate);
			tuple3List.add(tuple3);
		}
		Map<State, List<Tuple3<Date, Date, Date>>> stateMap = new HashMap<State, List<Tuple3<Date, Date, Date>>>();
		stateMap.put(State.COMPLETION, tuple3List);
		stateMap.put(State.VERIFICATION, tuple3List);
		List<String> streetRegionIdList = regionService.getStreetRegionIdById(regionId);
		
		Map<String, Map<State, List<Tuple3<Date, Date, Date>>>> resultMap = new HashMap<String, Map<State, List<Tuple3<Date, Date, Date>>>>();
		
		for(String id : streetRegionIdList) {
			resultMap.put(id, stateMap);
		}
		return resultMap;
	}

	 /**
     * 请求参数：审核状态，受理时间区间。响应参数：申请编号列表。
     * @param stateId 状态标识符
     * @param from 受理时间区间下限
     * @param to 受理时间区间上限
     * @return 申请编号列表
     */
	@Override
	public List<String> getApplyIdsByStateDuring(int stateId, Date from, Date to) {
		// TODO Auto-generated method stub
		List<CheckTotal> checkTotalList = checkTotalService.queryAll();
		List<String> result = new ArrayList<String>();
		for(CheckTotal checkTotal : checkTotalList) {
			result.add(checkTotal.getId());
		}
		return result;
	}

}
